<template>
  <div class="category_container">
    <div class="left_container">
      <div class="left_wrapper">
        <LeftItem v-for="(item,index) in categoryItems" 
       :key="index"
       :item="item"
       :index="index"
       :selectIndex="selectIndex"
       @changeSelectIndex="changeSelectIndex"/>
      </div>
    </div>
    <div class="right_container"
    ref="right"
    @touchstart="touchStart">
      <div class="right_wrrapper">
        <div  v-for="(item,index) in categoryItems"
        :key="index"
        class="right_item_block_container"
        ref="block">
          <div class="title">{{item.cname}}</div>
          <div class="right_iteem_block_wrapper">
            <RightItem v-for="(item,index) in item.subcategories"
            :key="index"
            :item="item"/>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import LeftItem from "../components/LeftItem";
import RightItem from "../components/RightItem";

export default {
  components:{
    LeftItem,
    RightItem
  },
  data(){
    return{
      categoryItems:[],
      selectIndex:0
    }
  },
  methods:{
    changeSelectIndex(index){
      this.$refs.right.removeEventListener("scroll",this.handleScroll)
      this.selectIndex  = index;
      var blockArray = this.$refs.block;
      // console.log(blockArray);
      var targetBlock  = blockArray[this.selectIndex];
      targetBlock.scrollIntoView({behavior:"smooth"});

    },
    handleScroll(){
    
      // this.selectIndex
      //获取 滚动 距离
      var scrollOffset = event.target.scrollTop;
      // console.log(scrollOffset) 
      var blockArray = this.$refs.block;

      for(let index=0;index<blockArray.length;index++){
        if(scrollOffset>=blockArray[index].offsetTop&&scrollOffset<blockArray[index+1].offsetTop){
          this.selectIndex = index;
        }
      }
    },
    touchStart(){
      this.$refs.right.addEventListener("scroll",this.handleScroll)
    }
  },
  mounted(){
    axios.get("http://api.kudesoft.cn/tdk/category").then((res)=>{
      // console.log(res)
      this.categoryItems = res.data.data.data;
    }).catch(()=>{

    })

    this.$refs.right.addEventListener("scroll",this.handleScroll)
  }
}
</script>

<style scoped>
*{
  padding: 0;
  margin: 0;
}
.category_container{
  width: 100%;
  height: 100vh;
  display: inline-flex;
  justify-content: center;
  align-items: center;
}

.left_container{
  height: 100%;
  width: 25%;
  /* font-size: 20px; */
  color: teal;
  overflow: scroll;
}


.right_container{
  height: 100%;
  width: 73%;
  overflow: scroll;
}

.right_wrrapper{
  width: 100%;
  display: inline-flex;
  justify-content: flex-start;
  align-items: flex-start;
  flex-wrap: wrap;
}

.right_item_block_container{
  width: 100%;
  border: 1px solid blueviolet;
}

.right_iteem_block_wrapper{
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  flex-wrap: wrap;
}

.title{
  height: 40px;
}
</style>